package com.ugoodtech.mdcc.web.controller;

import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.dto.ComplaintSearch;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import com.ugoodtech.mdcc.core.service.ComplaintKPIService;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.ComplaintReportService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.security.Principal;


@Api(description = "投诉单相关接口")
@RestController
@RequestMapping("/complaintReport")
public class ComplaintReportController extends BaseController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ComplaintKPIService complaintKPIService;

    @Autowired
    private ComplaintReportService complaintReportService;


    @ApiOperation("统计投诉报告KPI")
    @RequestMapping(value = "getComplaintKPI", method = RequestMethod.POST)
    public JsonResponse getComplaintKPI(@RequestParam Long loginUserId,
                                        @ApiParam("投诉单筛选条件") @RequestBody ComplaintSearch complaintSearch) {
        User user = userRepository.findOne(loginUserId);
        if (user == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        Object object = complaintKPIService.statComplaint(complaintSearch, user);
        return JsonResponse.successResponseWithData(object);
    }


    @ApiOperation("查询投诉报告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "getComplaintReport", method = RequestMethod.POST)
    public JsonResponse getComplaintReport(@RequestParam Long loginUserId, Pageable pageable,
                                           @ApiParam("投诉单筛选条件") @RequestBody ComplaintSearch complaintSearch) {
        User user = userRepository.findOne(loginUserId);
        if (user == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        return complaintReportService.getComplaintReport(pageable, complaintSearch, user);
    }

    @ApiOperation("编辑投诉报告")
    @RequestMapping(value = "editComplaintReport", method = RequestMethod.POST)
    public JsonResponse editComplaintReport(@RequestParam("complaintReportId") Long complaintReportId,
                                            @RequestParam("questionId") Long questionId,
                                            @RequestParam(value = "answerId", required = false) String answerId,
                                            @RequestParam(value = "answerOther", required = false) String answerOther,
                                            @RequestParam("loginUserId") Long loginUserId) {
        User user = userRepository.findOne(loginUserId);
        if (user == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        if (StringUtils.isEmpty(answerId) && StringUtils.isEmpty(answerOther)) {
            return JsonResponse.errorResponseWithError("抱歉", "题目都是必填的");
        }
        return complaintReportService.editComplaintReport(complaintReportId, questionId, answerId, answerOther);
    }

    @ApiOperation("投诉报告推送微信")
    @RequestMapping(value = "sendComplaintMessage", method = RequestMethod.GET)
    public JsonResponse sendComplaintReportMsg(@ApiParam("登陆用户ID") @RequestParam(value = "loginUserId") Long loginUserId,
                                               @ApiParam("报告ID") @RequestParam(value = "reportId") Long reportId,
                                               @ApiParam("用户ID") @RequestParam(value = "userIds") String userIds) {
        isUserLogin();
        User user = userRepository.findOne(loginUserId);
        if (user == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到当前登陆用户");
        }
        if (reportId == null || reportId == 0) {
            return JsonResponse.errorResponseWithError("抱歉", "报告ID数据异常");
        }
        return complaintReportService.sendComplaintReportMsg(reportId, userIds);
    }

}
